package com.miyake.client.profile;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;

public class UserPropertyList extends Composite {
	private CellTable<UserElement> cellTable;
	protected DoctorsUserServiceAsync service = DoctorsUserService.Util.getInstance();

	public UserPropertyList(boolean editable) {
		cellTable = new CellTable<UserElement>();
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.add(cellTable);
		cellTable.setWidth("400px");
		
		TextColumn<UserElement> textColumn = new TextColumn<UserElement>() {
			@Override
			public String getValue(UserElement object) {
				return object.getTitle();
			}
		};
		cellTable.setColumnWidth(textColumn, "100px");
		cellTable.addColumn(textColumn, "");
		
		if (editable) {
			Column<UserElement, String> column = new Column<UserElement, String>(new EditTextCell()) {
				@Override
				public String getValue(UserElement object) {
					return (String) object.getValue();
				}
			};
			column.setFieldUpdater(new FieldUpdater<UserElement, String>() {
				@Override
				public void update(int index, UserElement object, String value) {
					setUserProperty(index, value);
				}
			});
			cellTable.setColumnWidth(column, "100px");
			cellTable.addColumn(column, "");
		}
		else {
			TextColumn<UserElement> column = new TextColumn<UserElement>() {
				@Override
				public String getValue(UserElement object) {
					return object.getValue();
				}
			};
			cellTable.setColumnWidth(column, "100px");
			cellTable.addColumn(column, "");
		}

	}

	protected void setUserProperty(int index, String value) {
		service.updateMyProperty(index, value, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {
				
			}
		});
	}


	public void setUserProperty(UserProperty prop) {
		cellTable.setRowData(prop.getProperties());
	}
}
